Version Control

👤Author name

Science School of Quantitative Ecology 2025

bit.ly/SSoQE

This presentation


Learning Objectives

What we’ll cover today

🎯 Main Topics

  • What is version control?
  • Git/GitHub setup
  • Make a record of a change (commit)
  • Sync changes with remote server

By the end, you’ll be able to:

  • Use git in RStudio
  • Understand basic git terminology
  • Create a GitHub repo
  • Make a commit
  • Push and Pull changes
  • Handle a merge conflict

What is Version Control? 🤔

It is all about keeping track of changes 📓✍️

Discussion

03:00

Do you recognize some of these questions?

  • It broke … hopefully I have a working version somewhere?
  • Can you please send me the latest version?
  • Which version are you using?
  • I am sure it used to work. When did it change?
  • My laptop is gone. Is my data now gone?

How do you keep track of changes?

Fundamentals

Git

  • local software
  • keep track of changes of files


GitHub

  • host server
  • store (git) the data
  • project management, collaboration, publishing

Git/GitHub setup AKA “git hell”

Follow instructions in Version Control - git hell (a separate presentation).

Getting all the necessary software installed, configured, and playing nicely together is honestly half the battle … Brace yourself for some pain

Weapon of choice (GUI)

You will still need to know the basics of shell.

GUI = Graphical User Interface

I will be showing you how to use:

Note on {usethis} package

The {usethis} package is a collection of functions that help various tasks in R.

Install the package by running:

install.packages("usethis")

call a function by running:

usethis::function_name()

Basic vocabulary

  • R script is a record of code.
  • R Project is self contained project/study/paper containg R scripts, data, figures, etc.
  • Every such project is called repository (ie a repo)
  • Your local repository is called local
  • Your online repository, is called remote




Git init (project first)

Activate git for a repo

  1. For existing project
usethis::use_git()
  1. Create new project with git tracking (either via RStudio GUI or via {usethis} package):
usethis::create_project("<DIRECTORY>")
# switch to the new project
usethis::use_git()

Git integration is automatic in Source control panel

Create new project with git tracking

For existing project

git init

Create new project with git tracking

git init <DIRECTORY>

Note on practical exercises

Practical Exercise


  • Make a new project with Git tracking

05:00

A record of a change

a commit

A commit is a record of a change

If you create or edit a file in your repository and save the changes, you need to record your change via a commit

Chess analogy?

Chess move diary:

  • Bc4 (Bishop to c4)
  • Nf3 (Knight to f3)
  • Qc7 (Queen to c7)

a commit

Pawn to d4

Edit line 32 of file A

a commit



3 states of a file


Staging changes

Make a change to a file and save it. Now stage the change:

  • two yellow ?? indicates adding a file
  • a blue M indicates edit a file that has already been committed
  • a red D indicates deleting a file

  • The red icon indicates removed files.
  • The yellow icon indicates modified files.
  • The green icon indicates added files.
git add <FILE>

Practical Exercise


  1. Make changes to (a) file(s)
  2. Make a new file
  3. Stage and Unstage the changes

05:00

a first commit

Commit (record) staged changes:

git commit -am "commit message"

Review history

$ git log --stat
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    Remove unnecessary test

 lib/simplegit.rb | 5 -----
 1 file changed, 5 deletions(-)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 10:31:28 2008 -0700

    Initial commit

 README           |  6 ++++++
 Rakefile         | 23 +++++++++++++++++++++++
 lib/simplegit.rb | 25 +++++++++++++++++++++++++
 3 files changed, 54 insertions(+)

Dissecting a commit

SHA - unique identifier

Author - who has done this?

Date - when was this done?

Message - description of what has been done

Stats - what has changed?

Practical Exercise


  1. commit some changes
  2. review history

05:00

Commit message

Commits are quick and cheap. Therefore:

  1. commit often (!)
  2. provide useful commit messages.

Commit history

Remote

remote

Update remote - PUSH

Now we need to sync chnages with the remote using PUSH

Add a remote to existing local repo (only once):

usethis::use_github()

Push local to remote (GitHub):

Add a remote to existing local repo (only once):

Push local to remote (GitHub):

Add a remote to existing local repo (only once):

Push local to remote (GitHub):

Add a remote to existing local repo (only once):

git remote add origin https://github.com/<OWNER>/<REPO>

Push local to remote (GitHub):

git push

Practical Exercise


  1. Publish repo to GitHub
  2. make new commit(s)
  3. Push changes to remote

05:00

update local- PULL

update local- PULL

update local- PULL

Now we need to sync chnages from the remote to local the using PULL

Pull from remote (GitHub) to local

Pull from remote (GitHub) to local

Pull from remote (GitHub) to local

Pull from remote (GitHub) to local:

git pull

GitHub intermezzo

A GitHub repo

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

GitHub creating a repo (repo first)

README - description of the project

.gitignore - list of files ignored by GitHub (more about it later)

license - tell other what they can do wit your code

GitHub creating a repo (repo first)

Practical Exercise


  1. Create a new repo on GitHub
  2. Delete a repo on GitHub

05:00

Git Clone (repo first)

Git clone (repo first)

Copy (download) from remote repo to local machine

Example of online repo: SSoQE/VersionControl-playground

Clone repo using new project in RStudio GUI or via {usethis} package:

usethis::create_from_github(
  repo_spec = "https://github.com/<OWNER>/<REPO>.git",
  destdir = "<DIRECTORY>",
  fork = FALSE
  )

Open Command Palette (Ctrl+Shift+p)

Paste in URL: "https://github.com/<OWNER>/<REPO>.git"

git clone https://github.com/<OWNER>/<REPO>.git <DIRECTORY>

Practical Exercise


clone a repo (e.g. any repo from SSoQE)

05:00

Merge conflict

Merge conflict 💩💩💩

A merge conflict can occur when you are changing the same line in one file differently.

Merge conflict 💩💩💩

To https://github.com/picardis/myrepo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/picardis/myrepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

a good strategy to avoid such conflicts:

  • Commit often
  • Work in small steps
  • Push and pull regularly
  • Organize your code in small modules (scripts)


Merge conflicts cannot always be avoided (but can be mitigated by branches - NOT PART OF THIS LECTURE).

Merge conflict 💩💩💩

If you have questions, please
<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>> branch-a

Delete the unwanted text (including the decorations)

If you have questions, please
ask your question in IRC.

Then save the file, stage, and commit again

Ups! I have made a mistake 😮

How to undo last commit?

Variant A: I commited but NOT pushed yet.

RStudio has a range of possibilities to work with Git and GitHub as shown in this tutorial. The Terminal (NOT console) has more commands and options and will be handy for trouble shooting.

git reset --soft HEAD@{1}

Open Command Palette (Ctrl+Shift+p)

Write Git: Undo Last Commit

git reset --soft HEAD@{1}

Ups! I have made a mistake 😮

How to undo last commit?

Variant B: I commited but AND pushed already.

We need the Terminal (NOT console) again.

Copy the SHA of the last commit

git reset --hard <SHA>

Need to push the changes to the remote repo:

git push --force

In the Source control panel -> COMMITS section -> Right-click on the commit you want to revert to -> Select the Reset Current Branch to Previous Commit

Right-click on the commit you would like to undo to and select Revert a commit.

Copy the SHA of the last commit

git reset --hard <SHA>

Need to push the changes to the remote repo:

git push --force

Outro

SPROuT

The materials used in this presentation are further expanded in a greater detail in course Science Powered through Reproducibility, Openness, and Teamwork (SPRouT) taught at Charles University.

About me

Ondřej Mottl Assistant Professor at Charles University

Head of the 🧑‍💻 Laboratory of Quantitative Ecology